package com.xm.settlement.controller;

import com.xm.settlement.commons.base.BaseController;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.model.WechatService;
import com.xm.settlement.service.WechatSerService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangshi on 2017/4/26.
 */
@Controller
@RequestMapping("/wechatService")
public class WechatServiceController extends BaseController {

    @Autowired
    private WechatSerService wechatSerService;

    /**
     * 微信客服管理主页
     *
     * @return
     */
    @GetMapping(value = "/manage")
    public String Manager() {
        return "settlement/wechatService/wechatServiceManage";
    }

    /**
     * 微信客服管理
     *
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        pageInfo.setCondition(condition);
        wechatSerService.selectWeChatServiceList(pageInfo);
        return pageInfo;
    }


    /**
     * 微信客服添加主页
     *
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "dms/wechatService/wechatServiceAdd";
    }

    /**
     * 添加微信客服
     *
     * @param wechatService
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Object add(WechatService wechatService) {
        int index = wechatService.getIndex();
       /* int count=wechatSerService.selectCount(wechatService);*/
        if (wechatSerService.selectWechatServiceByIndex(index) != null) {
            return renderError("类型编号重复");
        }
        wechatService.setCreateId(getUserId());
        wechatService.setCreateTime(new Date());
        wechatService.setUpdateId(getUserId());
        wechatService.setUpdateTime(new Date());
        wechatSerService.insert(wechatService);
        return renderSuccess("添加成功");
    }


    /**
     * 微信客服编辑主页
     *
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
        WechatService wechatService = wechatSerService.selectById(id);
        model.addAttribute("wechatService", wechatService);
        return "settlement/wechatService/wechatServiceEdit";
    }


    /**
     * 编辑微信客服
     *
     * @param wechatService
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(WechatService wechatService) {
        wechatService.setUpdateTime(new Date());
        wechatService.setUpdateId(getUserId());
        wechatService.setTitleImg(wechatService.getTitleImg());
        if (wechatSerService.updateById(wechatService)) {
            return renderSuccess("编辑成功！");
        }
        return renderError("编辑失败！");
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        return (wechatSerService.deleteById(id)) ? renderSuccess("删除成功！") : renderError("删除失败！");
    }


    /**
     * 图片上传
     */
    @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadPhoto(@RequestParam("Filedata") MultipartFile multiFile, HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletContext sc = request.getSession().getServletContext();
            String dir = sc.getRealPath("/upload");//附件存放服务器的路径
            File file = new File(dir);
            if (!file.exists()) {
                file.mkdirs();
            }
            String filename = multiFile.getOriginalFilename();
            String realname = filename.substring(0, filename.indexOf("."));
            //防止文件被覆盖，以纳秒生成文件
            Long _l = System.nanoTime();
            String _extfilename = filename.substring(filename.indexOf("."));
            filename = _l + _extfilename;
            String uploadPath = request.getContextPath() + "" + dir + "/" + filename;
            FileUtils.writeByteArrayToFile(new File(dir, filename), multiFile.getBytes());
            return renderSuccess("upload" + "/" + filename);
        } catch (IOException io) {
            io.printStackTrace();
            logger.info(io);
            return renderError("fail");
        }
    }

}
